<?php
require 'model-db-connection/config.php';
$config = require 'model-db-connection/qrc_conf.properties.php';
$searchCondition = $_GET['searchCondition'];
$project_id = $_GET['project_id'];
$project_code = $_GET['project_code'];
$document_no = $_GET['document_no'];
$po_no = $_GET['po_no'];
$po_owner = $_GET['po_owner'];
$po_sender = $_GET['po_sender'];
$po_issue_date = $_GET['po_issue_date'];
$po_order_type = $_GET['po_order_type'];
?>
<div class="widget">
    <div class="widget-head">
        <h4 class="heading">Member (ลูกทีม)</h4>
        <a href="#modal-po" data-toggle="modal" class="btn btn-xs btn-inverse pull-right" id="new_po_btn" style="margin-top: 6px;"><i class="fa fa-plus fa-fw"></i> New PO</a>
        <div class="clearfix"></div>
    </div>
    <div class="widget-body innerAll inner-2x">
        <!-- Table -->
        <table class="footable table table-striped table-primary dynamicTable tableTools" id="dataTables-example">

            <!-- Table heading -->
            <thead>
                <tr>
                    <th data-class="expand" class="center">Project Name</th>
                    <th class = "center">Project Code</th>
                    <th class = "center">Document No</th>
                    <th class = "center">PO No.</th>
                    <th class = "center">PO Owner</th>
                    <th class = "center">PO Sender</th>
                    <th data-hide="phone,tablet">Home Plan</th>
                    <th data-hide="phone,tablet">Home Plot</th>
                    <th data-hide="phone,tablet">Issue Date</th>
                    <th data-hide="phone,tablet">Order type</th>
                    <th data-hide="phone,tablet">Quantity</th>
                    <th data-hide="phone,tablet">Plan Size</th>
                    <th data-hide="phone,tablet">Unit Prices</th>
                    <th data-hide="phone,tablet">Amount</th>
                    <th data-hide="phone,tablet">Grand Total included VAT 7%</th>
                    <th data-hide="phone,tablet">Supervisor Control</th>
                    <th data-hide="phone,tablet">Project Manager</th>
                    <th data-hide="phone,tablet">Project Foreman</th>
                    <th data-hide="phone,tablet">Remark</th>
                    <th data-hide="phone,tablet">PO Image</th>
                    <th></th>
                </tr>
            </thead>
            <!-- // Table heading END -->
            <!-- Table body -->
            <tbody>
                <?php
                if ($searchCondition == "All") {
                    $sqlSelectMemberAll = "SELECT qp.PO_PROJECT_NAME as PO_PROJECT_NAME,"
                            . "qp.PO_PROJECT_CODE as PO_PROJECT_CODE,"
                            . "qp.PO_DOCUMENT_NO as PO_DOCUMENT_NO,"
                            . "qp.PO_PO_NO as PO_PO_NO,"
                            . "qp.PO_HOME_PLAN as PO_HOME_PLAN,"
                            . "qp.PO_HOME_PLOT as PO_HOME_PLOT,"
                            . "qp.PO_OWNER as PO_OWNER,"
                            . "qp.PO_SENDER as PO_SENDER,"
                            . "qp.PO_ISSUE_DATE as PO_ISSUE_DATE,"
                            . "qts.service_name as order_type_name,"
                            . "qp.PO_QUANTITY as PO_QUANTITY,"
                            . "qp.PO_PLAN_SIZE as PO_PLAN_SIZE,"
                            . "qp.PO_UNIT_PRICE as PO_UNIT_PRICE,"
                            . "qp.PO_AMOUNT as PO_AMOUNT,"
                            . "qp.PO_VAT as  PO_VAT,"
                            . "qp.PO_SUPERVISOR_ID as PO_SUPERVISOR_ID,"
                            . "qp.PO_PROJECT_MANAGER_ID as PO_PROJECT_MANAGER_ID,"
                            . "qp.PO_PROJECT_FOREMAN_ID as PO_PROJECT_FOREMAN_ID,"
                            . "qp.PO_ID as PO_ID,"
                            . "qp.PO_REMARK as PO_REMARK"
                            . " FROM QRC_PO qp"
                            . " LEFT JOIN qrc_type_of_service qts on qp.po_order_type_id = qts.service_id;";
                } else {
                    $checkProjectid = !empty($project_id) ? " AND qp.PO_PROJECT_CODE LIKE '%$project_id%'" : "";
                    $checkProjecCode = !empty($project_code) ? " AND qp.PO_PROJECT_CODE LIKE '%$project_code%'" : "";
                    $checkDocNo = !empty($document_no) ? " AND qp.PO_DOCUMENT_NO LIKE '%$document_no%'" : "";
                    $checkPoNo = !empty($po_no) ? " AND qp.PO_PO_NO LIKE '%$po_no%'" : "";
                    $checkPoOwner = !empty($po_owner) ? " AND qp.PO_OWNER LIKE '%$po_owner%'" : "";
                    $checkPoSender = !empty($po_sender) ? " AND qp.PO_SENDER LIKE '%$po_sender%'" : "";
                    $checkPoIssueDate = !empty($po_issue_date) ? " AND qp.PO_ISSUE_DATE BETWEEN '$po_issue_date' AND '$po_issue_date'" : "";
                    $checkPoOrderType = !empty($po_order_type) ? " AND qp.PO_ORDER_TYPE_ID LIKE '$po_order_type'" : "";
                    $sqlSelectMemberAll = "SELECT qp.PO_PROJECT_NAME as PO_PROJECT_NAME,"
                            . "qp.PO_PROJECT_CODE as PO_PROJECT_CODE,"
                            . "qp.PO_DOCUMENT_NO as PO_DOCUMENT_NO,"
                            . "qp.PO_PO_NO as PO_PO_NO,"
                            . "qp.PO_HOME_PLAN as PO_HOME_PLAN,"
                            . "qp.PO_HOME_PLOT as PO_HOME_PLOT,"
                            . "qp.PO_OWNER as PO_OWNER,"
                            . "qp.PO_SENDER as PO_SENDER,"
                            . "qp.PO_ISSUE_DATE as PO_ISSUE_DATE,"
                            . "qts.service_name as order_type_name,"
                            . "qp.PO_QUANTITY as PO_QUANTITY,"
                            . "qp.PO_PLAN_SIZE as PO_PLAN_SIZE,"
                            . "qp.PO_UNIT_PRICE as PO_UNIT_PRICE,"
                            . "qp.PO_AMOUNT as PO_AMOUNT,"
                            . "qp.PO_VAT as  PO_VAT,"
                            . "qp.PO_SUPERVISOR_ID as PO_SUPERVISOR_ID,"
                            . "qp.PO_PROJECT_MANAGER_ID as PO_PROJECT_MANAGER_ID,"
                            . "qp.PO_PROJECT_FOREMAN_ID as PO_PROJECT_FOREMAN_ID,"
                            . "qp.PO_ID as PO_ID,"
                            . "qp.PO_REMARK as PO_REMARK"
                            . " FROM QRC_PO qp"
                            . " LEFT JOIN qrc_type_of_service qts on qp.po_order_type_id = qts.service_id"
                            . " WHERE 1=1"
                            . $checkProjectid
                            . $checkProjecCode
                            . $checkDocNo
                            . $checkPoNo
                            . $checkPoOwner
                            . $checkPoSender
                            . $checkPoIssueDate
                            . $checkPoOrderType;
                }
//                echo $sqlSelectMemberAll;
                $sqlGetAllData = mysql_query($sqlSelectMemberAll);
                if (mysql_num_rows($sqlGetAllData) >= 1) {
                    while ($row = mysql_fetch_assoc($sqlGetAllData)) {
                        echo '<tr class = "gradeX">';
                        echo '<td>' . $row['PO_PROJECT_NAME'] . '</td>';
                        echo '<td>' . $row['PO_PROJECT_CODE'] . '</td>';
                        echo '<td>' . $row['PO_DOCUMENT_NO'] . '</td>';
                        echo '<td>' . $row['PO_PO_NO'] . '</td>';
                        echo '<td>' . $row['PO_OWNER'] . '</td>';
                        echo '<td>' . $row['PO_SENDER'] . '</td>';
                        echo '<td>' . $row['PO_HOME_PLAN'] . '</td>';
                        echo '<td>' . $row['PO_HOME_PLOT'] . '</td>';
                        echo '<td>' . $row['PO_ISSUE_DATE'] . '</td>';
                        echo '<td>' . $row['order_type_name'] . '</td>';
                        echo '<td>' . $row['PO_QUANTITY'] . '</td>';
                        echo '<td>' . $row['PO_PLAN_SIZE'] . '</td>';
                        echo '<td>' . $row['PO_UNIT_PRICE'] . '</td>';
                        echo '<td>' . $row['PO_AMOUNT'] . '</td>';
                        echo '<td>' . $row['PO_VAT'] . '</td>';
                        echo '<td>' . $row['PO_SUPERVISOR_ID'] . '</td>';
                        echo '<td>' . $row['PO_PROJECT_MANAGER_ID'] . '</td>';
                        echo '<td>' . $row['PO_PROJECT_FOREMAN_ID'] . '</td>';
                        echo '<td>' . $row['PO_REMARK'] . '</td>';
                        $sqlSelectImageByID = "SELECT IMAGE_PATH as IMAGE_PATH FROM QRC_PO_IMAGE WHERE TEMP_PO_ID LIKE '" . $row['PO_ID'] . "'";
                        $queryGetFilePath = mysql_query($sqlSelectImageByID);
                        $strBuilding = "";
                        while ($rowq = mysql_fetch_assoc($queryGetFilePath)) {
                            $strBuilding.='<d class="fancybox-effects-d" href="image/uploads/' . $rowq['IMAGE_PATH'] . '" title="' . $rowq['IMAGE_PATH'] . '" onlick="changeAttrHref()"><img src="image/uploads/' . $rowq['IMAGE_PATH'] . '" alt="Smiley face" width="200"></d>';
                        }
                        echo '<td>' . $strBuilding . '</td>';

                        echo '<td class = "center">';
                        echo '<a href = "#modal-po" data-toggle = "modal" class = "btn btn-primary" onclick=editPO("' . $row['PO_ID'] . '")><i class = "icon-document-line"></i> Edit</a>';
                        echo '<button class = "btn btn-danger" style="margin-left: 5px" onclick=deletePO("' . $row['PO_ID'] . '","' . $row['PO_PO_NO'] . '")><i class = "fa fa-trash-o"></i> Delete</button>';

                        echo '</td>';
                        echo '</tr>';
                        $strBuilding = "";
                    }
                }
                ?>
            </tbody>
            <!-- // Table body END -->

        </table>
        <!-- // Table END -->

    </div>
</div>
<script src="assets/components/common/tables/responsive/assets/lib/js/footable.min.js?v=v1.0.2&sv=v0.0.1"></script>
<script src="assets/components/common/tables/responsive/assets/custom/js/tables-responsive-footable.init.js?v=v1.0.2&sv=v0.0.1"></script>
<script src="assets/components/modules/admin/notifications/gritter/assets/lib/js/jquery.gritter.min.js?v=v1.0.2&sv=v0.0.1"></script>
<script src="assets/components/modules/admin/modals/assets/js/bootbox.min.js?v=v1.0.2&sv=v0.0.1"></script>
<script src="assets/components/common/forms/elements/multiselect/assets/lib/js/jquery.multi-select.js?v=v1.0.2&sv=v0.0.1"></script>
<script src="assets/components/common/forms/elements/multiselect/assets/custom/js/multiselect.init.js?v=v1.0.2&sv=v0.0.1"></script>
<script src="assets/components/common/forms/elements/select2/assets/lib/js/select2.js?v=v1.0.2&sv=v0.0.1"></script>
<script src="assets/components/common/forms/elements/select2/assets/custom/js/select2.init.js?v=v1.0.2&sv=v0.0.1"></script>

<div class="modal fade" id="modal-po">

    <div class="modal-dialog">
        <div class="modal-content" style="width: 900px">

            <!-- Modal heading -->
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h2 class="modal-title">Member Create/Edit</h2>
            </div>
            <!-- // Modal heading END -->

            <!-- Modal body -->
            <div class="modal-body">
                <div class="innerAll">
                    <div class="innerLR">
                        <table width="100%">
                            <tr>
                                <td align="right" style="width:250px">Project Name (ชื่อโครงการ):</td>
                                <td align="left" style="width:250px">
                                    <!--<input type="text" class="form-control" id="team_code_search">-->
                                    <select class="form-control" id="po_project_name_form" name="po_project_name_form">
                                        <option value=""></option>
                                        <?php
                                        $sqlSelectMemType = "SELECT * FROM QRC_PROJECT;";
                                        $resultSets = mysql_query($sqlSelectMemType);
                                        while ($row = mysql_fetch_array($resultSets)) {
                                            echo '<option value="' . $row['project_name'] . '">' . $row['project_name'] . '</option>';
                                        }
                                        ?>
                                    </select>
                                </td>
                                <td align="right" style="width:250px">Project Code (หมายเลขโครงการ):</td>
                                <td align="left" style="width:250px"><input type="text" class="form-control" id="po_project_code_form" disabled="true"></td>
                            </tr>
                            <tr>
                                <td align="right" style="width:250px">Document No. (เลขที่):</td>
                                <td align="left" style="width:250px">
                                    <input type="text" class="form-control" id="po_document_no_form">
                                </td>
                                <td align="right" style="width:250px">PO No. (เลขที่ใบสั่งจ้าง):</td>
                                <td align="left" style="width:250px"><input type="text" class="form-control" id="po_po_no_form"></td>
                            </tr>
                            <tr>
                                <td align="right" style="width:250px">Plan (แบบบ้าน):</td>
                                <td align="left" style="width:250px">
                                    <select class="form-control" id="po_home_plan_form" name="po_home_plan_form">
                                        <option value="0"></option>
                                        <option value="LN01">LN01</option>
                                        <option value="LN02">LN02</option>
                                        <option value="AT021">AT021</option>
                                    </select>
                                </td>
                                <td align="right" style="width:250px">Plot (แปลงบ้าน):</td>
                                <td align="left" style="width:250px">
                                    <select class="form-control" id="po_home_plot_form" name="po_home_plot_form">
                                        <option value="0"></option>
                                        <option value="A02">A02</option>
                                        <option value="A03">A03</option>
                                        <option value="89">89</option>
                                        <option value="90">90</option>
                                    </select>
                                </td>
                            </tr>
                            <tr>
                                <td align="right" style="width:250px">PO Owner (เจ้าของ PO):</td>
                                <td align="left" style="width:250px">
                                    <input type="text" class="form-control" id="po_owner_form">
                                </td>
                                <td align="right" style="width:250px">PO Sender (จนท. PO):</td>
                                <td align="left" style="width:250px"><input type="text" class="form-control" id="po_sender_form"></td>
                            </tr>
                            <tr>
                                <td align="right" style="width:250px">Issue Date (วันที่):</td>
                                <td align="left" style="width:250px"><input type="text" class="form-control" id="po_issue_date_form" data-date-format="yyyy-mm-dd" placeholder="yyyy-mm-dd"></td>
                                <td align="right" style="width:250px">Order Type (ประเภทงาน):</td>
                                <td align="left" style="width:250px">
                                    <select class="form-control" id="po_order_type_form" name="po_order_type_form">
                                        <option value=""></option>
                                        <?php
                                        $sqlSelectMemType = "SELECT * FROM QRC_TYPE_OF_SERVICE;";
                                        $resultSets = mysql_query($sqlSelectMemType);
                                        while ($row = mysql_fetch_array($resultSets)) {
                                            echo '<option value="' . $row['service_id'] . '">' . $row['service_name'] . '</option>';
                                        }
                                        ?>
                                    </select>
                                </td>
                            </tr>
                            <tr>
                                <td align="right" style="width:250px">Quantity (จำนวน):</td>
                                <td align="left" style="width:250px"><input type="text" class="form-control" id="po_quantity_form"></td>
                                <td align="right" style="width:250px">Plan Size (ขนาด ตร.ม.):</td>
                                <td align="left" style="width:250px"><input type="text" class="form-control" id="po_plan_size_form"></td>
                            </tr>
                            <tr>
                                <td align="right" style="width:250px">Unit Price (ราคาต่อหน่วย):</td>
                                <td align="left" style="width:250px"><input type="text" class="form-control" id="po_unit_price_form"></td>
                                <td align="right" style="width:250px">Amount (ราคารวม):</td>
                                <td align="left" style="width:250px"><input type="text" class="form-control" id="po_amount_form"></td>
                            </tr>
                            <tr>
                                <td align="right" style="width:250px">Grand Total included VAT 7%:</td>
                                <td align="left" style="width:250px"><input type="text" class="form-control" id="po_vat_form"></td>
                                <td align="right" style="width:250px">Supervisor Control (ผู้ควบคุมงานของลูกค้า):</td>
                                <td align="left" style="width:250px"><input type="text" class="form-control" id="po_project_supervisor_form" disabled="true"></td>

                            </tr>
                            <tr>
                                <td align="right" style="width:250px">Project Manager (ผู้จัดการโครงการ):</td>
                                <td align="left" style="width:250px"><input type="text" class="form-control" id="po_project_manager_form" disabled="true"></td>
                                <td align="right" style="width:250px">Project Foreman (ผู้ควบคุมงานของโครงการ):</td>
                                <td align="left" style="width:250px"><input type="text" class="form-control" id="po_project_foreman_form" disabled="true"></td>
                            </tr>
                            <tr>
                                <td align="right" style="width:250px">Remark:</td>
                                <td align="left" colspan="3"><input type="text" class="form-control" id="remark_inform"></td>

                            </tr>
                            <tr id="uploadpart">
                                <td colspan="4" align="center">
                                    <div id="mulitplefileuploader">Upload</div>
                                    <div id="status"></div>
                                    <span id="uploadWarning" style="color:red"></span>
                                    <br/>
                                </td>
                            </tr>
                            <tr>
                                <td align="right" style="width:250px" colspan="2"><button class = "btn btn-primary" style="margin-left: 5px" id="po_submit_btn_form">Submit (บันทึก/แก้ไข)</button></td>
                                <td align="left" style="width:250px" colspan="2"><button class = "btn btn-primary" style="margin-left: 5px" id="po_reset_btn_form">Reset (รีเซต)</button></td>
                            </tr>
                        </table>
                    </div>
                </div>
            </div>
            <!-- // Modal body END -->

        </div>
    </div>
    <input type="hidden" id="hideMemSkill"/>
</div>
<script type="text/javascript">
    var createOrEditState = "Save";
    var settings;
    var i = 0;
    var poNo = "";
    var isUploadNewImage = 0;
    $(document).ready(function() {
        $('#dataTables-example').dataTable();
        $("d.fancybox-effects-d").fancybox();
        $("#uploadpart").hide();
        $('#po_po_no_form').keyup(function() {
            if ($("#po_po_no_form").val() === "") {
                $("#uploadpart").hide();
            } else {
                $("#uploadpart").show();
                poNo = $("#po_po_no_form").val();
            }
        });

        settings = {
            url: "model/upload_po_attach.php",
            dragDrop: true,
            fileName: "myfile",
            allowedTypes: "jpg,png,gif,doc,pdf,zip",
            returnType: "json",
            onSuccess: function(files, data, xhr)
            {
                isUploadNewImage = 1;
                $.gritter.add({
                    title: 'Success!',
                    text: data
                });
            },
            showDelete: true,
            deleteCallback: function(data, pd)
            {
                for (var i = 0; i < data.length; i++)
                {
                    $.post("model/delete_po_attach.php", {op: "delete", name: data[i]},
                    function(resp, textStatus, jqXHR)
                    {
                        $.gritter.add({
                            title: 'Success!',
                            text: resp
                        });
//                        $("#status").append(resp);
                    });
                }
                pd.statusbar.hide(); //You choice to hide/not.

            }
        }
        var uploadObj = $("#mulitplefileuploader").uploadFile(settings);

        $("#po_project_name_form").change(function() {
            blockPage();
            var tCode = $("#po_project_name_form").val();
            var jqxhr = $.post("model/GetTeamObject.php?tCode=" + tCode);
            jqxhr.success(function(data) {
                obj = JSON.parse(data);
                $("#po_project_code_form").val(obj.project_code);
                $("#po_project_manager_form").val(obj.project_manager);
                $("#po_project_supervisor_form").val(obj.supervisor);
                $("#po_project_foreman_form").val(obj.project_foreman);
                $.unblockUI();
            });
            jqxhr.error(function(data) {
                window.location.replace("error.php?error_msg=" + data);
            });
        });
        $("#new_po_btn").click(function() {
            createOrEditState = "Save";
        });
        $("#po_reset_btn_form").click(function() {
            blockPage();
            createOrEditState = "Save";
            $("#waringmsg").empty();
            $("#modal-po").modal('hide');
            setTimeout(function()
            {
                $("#load_po").load("PoMainPage.php", function() {
                    $.unblockUI();
                });
            }
            , 500);
        });
        $("#po_submit_btn_form").click(function() {
            blockPage();
            var project_name = $("#po_project_name_form").val();
            var project_code = $("#po_project_code_form").val();
            var doc_no = $("#po_document_no_form").val();
            var po_no = $("#po_po_no_form").val();
            var home_plan = $("#po_home_plan_form").val();
            var home_plot = $("#po_home_plot_form").val();
            var po_owner = $("#po_owner_form").val();
            var po_sender = $("#po_sender_form").val();
            var issue_date = $("#po_issue_date_form").val();
            var order_type = $("#po_order_type_form").val();
            var quantity = $("#po_quantity_form").val();
            var plan_size = $("#po_plan_size_form").val();
            var unit_price = $("#po_unit_price_form").val();
            var amount = $("#po_amount_form").val();
            var vat7 = $("#po_vat_form").val();
            var supervisor = $("#po_project_supervisor_form").val();
            var project_manager = $("#po_project_manager_form").val();
            var projectforeman = $("#po_project_foreman_form").val();
            var project_remark = $("#remark_inform").val();
            var data = "project_name=" + project_name
                    + "&project_code=" + project_code
                    + "&doc_no=" + doc_no
                    + "&po_no=" + po_no
                    + "&home_plan=" + home_plan
                    + "&home_plot=" + home_plot
                    + "&po_owner=" + po_owner
                    + "&po_sender=" + po_sender
                    + "&issue_date=" + issue_date
                    + "&order_type=" + order_type
                    + "&quantity=" + quantity
                    + "&plan_size=" + plan_size
                    + "&unit_price=" + unit_price
                    + "&amount=" + amount
                    + "&vat7=" + vat7
                    + "&supervisor=" + supervisor
                    + "&project_manager=" + project_manager
                    + "&projectforeman=" + projectforeman
                    + "&project_remark=" + project_remark;

            if (createOrEditState === "Edit") {
                if ($("#remark_inform").val() === "") {
                    $.unblockUI();
                    alert("กรุณาใส่ Remark");
                } else {
                    var isUploadImage = $.post("model/CheckImageUpload.php");
                    isUploadImage.success(function(resp) {
                        if (resp === "NO_DATA") {
                            var po_id = $("#hideMemSkill").val();
                            var updateWithSameImage = $.post("model/UpdatePoWithSameImage.php?" + data + "&po_id=" + po_id, function() {
                                $.unblockUI();
                            });
                            updateWithSameImage.success(function(updateResp) {

                                if (updateResp == 1) {
                                    $("#modal-po").modal('hide');
                                    setTimeout(function()
                                    {
                                        $("#load_po").load("PoMainPage.php", function() {
                                            $.unblockUI();
                                        });
                                        $.gritter.add({
                                            title: 'Success!',
                                            text: "<?= $config['msg_save_success'] ?>"
                                        });
                                    }
                                    , 500);
                                } else {
//                                    alert(updateResp);
                                }
                            });
                        } else {
//                            alert("New update image");
                            var po_id = $("#hideMemSkill").val();
                            var updateWithDifferImage = $.post("model/UpdatePoWithDifferImage.php?" + data + "&po_id=" + po_id, function() {
                                $.unblockUI();
                            });
                            updateWithDifferImage.success(function(updateResp) {

                                if (updateResp == 1) {
                                    $("#modal-po").modal('hide');
                                    setTimeout(function()
                                    {
                                        $("#load_po").load("PoMainPage.php", function() {
                                            $.unblockUI();
                                        });
                                        $.gritter.add({
                                            title: 'Success!',
                                            text: "<?= $config['msg_save_success'] ?>"
                                        });
                                    }
                                    , 500);
                                } else {
//                                    alert(updateResp);
                                }
                            });
                        }
                    });
                }
            } else {
                var jqxhr = $.post("model/SavingPo.php?" + data, function() {
                    $.unblockUI();
                });
                jqxhr.success(function(data) {

                    if (data == 1) {
                        $("#modal-po").modal('hide');
                        setTimeout(function()
                        {
                            $("#load_po").load("PoMainPage.php", function() {
                                $.unblockUI();
                            });
                            $.gritter.add({
                                title: 'Success!',
                                text: "<?= $config['msg_save_success'] ?>"
                            });
                        }
                        , 500);
                    } else {
                        alert(data);
                    }
                });
            }

        });
    });
    function blockPage() {
        $.blockUI({message: 'Operation in progress...', css: {
                border: 'none',
                padding: '15px',
                backgroundColor: '#000',
                '-webkit-border-radius': '10px',
                '-moz-border-radius': '10px',
                opacity: .5,
                color: '#fff'
            }});
    }
    function deletePO(PO_ID) {
        if (confirm("Are you sure?"))
        {
            blockPage();
            var jqxhr = $.post("model/DeletePO.php?PO_ID=" + PO_ID);
            jqxhr.success(function(data) {
//                if (data === 1) {
                setTimeout(function()
                {
                    $("#load_po").load("PoMainPage.php", function() {
                        $.unblockUI();
                    });

                    $.gritter.add({
                        title: 'Success!',
                        text: "<?= $config['msg_delete_success'] ?>"
                    });
                }
                , 500);
//                } else {
//                    alert("ไม่สามารถลบข้อมูลได้");
//                    $.unblockUI();
//                }
            });
            jqxhr.error(function(data) {
                window.location.replace("error.php?error_msg=" + data);
            });
        }
        else
        {
            e.preventDefault();
        }
    }
    function editPO(po_id) {
        blockPage();
        createOrEditState = "Edit";
        var jqxhr = $.post("model/GetPoByIDForEdit.php?po_id=" + po_id);
        jqxhr.success(function(data) {
            $("#uploadpart").show();
            $.unblockUI();
            obj = JSON.parse(data);
            $("#hideMemSkill").val(po_id);
            $("#uploadWarning").append("Leave empty for no change");
            $("#po_project_name_form").val(obj.PO_PROJECT_NAME);
            $("#po_project_code_form").val(obj.PO_PROJECT_CODE);
            $("#po_document_no_form").val(obj.PO_DOCUMENT_NO);
            $("#po_po_no_form").val(obj.PO_PO_NO);
            $("#po_home_plan_form").val(obj.PO_HOME_PLAN);
            $("#po_home_plot_form").val(obj.PO_HOME_PLOT);
            $("#po_owner_form").val(obj.PO_OWNER);
            $("#po_sender_form").val(obj.PO_SENDER);
            $("#po_issue_date_form").val(obj.PO_ISSUE_DATE);
            $("#po_order_type_form").val(obj.order_type_name);
            $("#po_quantity_form").val(obj.PO_QUANTITY);
            $("#po_plan_size_form").val(obj.PO_PLAN_SIZE);
            $("#po_unit_price_form").val(obj.PO_UNIT_PRICE);
            $("#po_amount_form").val(obj.PO_AMOUNT);
            $("#po_vat_form").val(obj.PO_VAT);
            $("#po_project_supervisor_form").val(obj.PO_SUPERVISOR_ID);
            $("#po_project_manager_form").val(obj.PO_PROJECT_MANAGER_ID);
            $("#po_project_foreman_form").val(obj.PO_PROJECT_FOREMAN_ID);
            $("#remark_inform").val(obj.PO_REMARK);
        });
        jqxhr.error(function(data) {
            window.location.replace("error.php?error_msg=" + data);
        });
    }
</script>